package com.itboye.lottery.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.itboye.bylibrary.widget.RatioImageView;
import com.itboye.lottery.R;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by wu on 2018/1/9.
 */

public class FindAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    ArrayList<HashMap<String, Object>> classEntityArrayList = new ArrayList<>();

    public FindAdapter(ArrayList<HashMap<String, Object>> classEntityArrayList) {
        this.classEntityArrayList = classEntityArrayList;
    }

    public OnItemClickListener itemClickListener;

    public void setOnItemClickListener(FindAdapter.OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }


    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        return new ViewHolderFind(LayoutInflater.from(context).inflate(R.layout.item_find, parent, false));
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        HashMap<String, Object> hash = classEntityArrayList.get(position);
        String title = hash.get("title").toString();
        String content = hash.get("content").toString();
        int resource = Integer.parseInt(hash.get("icon").toString());
        ((ViewHolderFind) holder).title.setText(title);
        ((ViewHolderFind) holder).content.setText(content);
        ((ViewHolderFind) holder).icon.setBackgroundResource(resource);
//        GlideUtil.display(context, list.get(position).getImg_url(), holder.img);
//        holder.name.setText(list.get(position).getClassification_name());
    }

    @Override
    public int getItemCount() {
        return classEntityArrayList == null ? 0 : classEntityArrayList.size();
    }

    public class ViewHolderFind extends RecyclerView.ViewHolder implements View.OnClickListener {
        public RatioImageView icon;
        public TextView title;
        public TextView content;

        public ViewHolderFind(View itemView) {
            super(itemView);
            icon = itemView.findViewById(R.id.iv_fragment_news_icon);
            title = itemView.findViewById(R.id.item_tv_news_title);
            content = itemView.findViewById(R.id.item_tv_find_content);
//
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (itemClickListener != null) {
                itemClickListener.onItemClick(v, getAdapterPosition());
            }
        }
    }

}
